﻿using System;
using System.Collections.Generic;
using System.Data.Entity;
using System.Linq;
using System.Web;

namespace DuelQuizzServer
{
    public partial class Notification
    {
        private static Entities context;
        private static DbSet<Notification> entities
        {
            get
            {
                if (Notification.context == null)
                {
                    Notification.context = new Entities();
                    Notification.context.Configuration.ProxyCreationEnabled = false;
                }
                return context.Notification;
            }
        }

        internal static void add(Notification notif)
        {
            Notification.entities.Add(notif);
            Notification.context.SaveChanges();
        }

        internal static List<Notification> getNotification(int user_id)
        {
            try
            {
                return entities.Where(p => (p.user_id == user_id)).OrderByDescending(p => p.date).ToList();
            }
            catch (Exception e)
            {
                return new List<Notification>();
            }
        }

        public override string ToString()
        {
            return "(" + date.Day + "-" + date.Month + "-" + date.Year + " " + date.Hour + ":" + date.Minute + ") " + message;

        }

        internal static void delete(int user_id)
        {
            foreach(Notification n in getNotification(user_id)){
                entities.Remove(n);
            }
            context.SaveChanges();

        }
    }
}